﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.Person.Modify;

namespace University.Workshop.Presentation.Person
{
    /// <summary>
    /// Interaction logic for ModifyPerson.xaml
    /// </summary>
    public partial class ModifyPerson : Page, IModifyPersonView
    {
        PresenterModifyPerson pModifyPerson;
        public ModifyPerson(Model.Person person)
        {
            InitializeComponent();
            pModifyPerson = new PresenterModifyPerson(this,person);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            pModifyPerson.UpdateViewData();
            App.Current.MainWindow.Title ="Modificar Persona";
        }
    

        public void  SystemMessage(string MessageValue)
        {
 	        MessageBox.Show(App.Current.MainWindow, MessageValue, "Atencion!!!", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }

        public void  ErrorMessage(string MessageValue)
        {
 	        MessageBox.Show(App.Current.MainWindow, MessageValue, "Error!!!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }

        #region Fields
        public string  PersonId
        {
	          get 
	        { 
		        return PersonCiTxt.Text.ToString();
	        }
	          set 
	        { 
		        PersonCiTxt.Text = value;
	        }
        }

        public string  PersonNames
        {
	          get 
	        { 
		        return PersonNamesTxt.Text.ToString();
	        }
	          set 
	        { 
		        PersonNamesTxt.Text = value; 
	        }
        }

        public string  PersonLastNames
        {
	          get 
	        { 
		        return PersonLastNamesTxt.Text.ToString(); 
	        }
	          set 
	        { 
		        PersonLastNamesTxt.Text = value;
	        }
        }

        public string  PersonNewPhones
        {
	          get 
	        { 
		        return PersonPhonesTxt.Text.ToString(); 
	        }
	          set 
	        { 
		        PersonPhonesTxt.Text = value;
	        }
        }

        public List<string>  PersonPhones
        {
	        set 
            {
                PersonPhonesList.ItemsSource = value;
            }
        }
        #endregion

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new SearchPerson());
        }

        private void ModifyBtn_Click(object sender, RoutedEventArgs e)
        {
            pModifyPerson.UpdatePerson();
        }

        public void ReturnSearchPerson()
        {
            this.NavigationService.Navigate(new SearchPerson());
        }

        #region validations
        private void PersonPhonesTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsDigit(ch) || ch.Equals(','));
            }
        }

        private void PersonPhonesTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        private void CantCupyPasteOrCut(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Cut)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Paste)
                e.Handled = true;
        }

        private void PersonPhonesTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            pModifyPerson.ChargePhonesToList();
        }

        private void JustLettersTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !char.IsLetter(ch);
            }
        }
        #endregion
    }
}
